home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / bitmap / reqmach.c < prev    next >
C/C++ Source or Header  |  1994-08-12  |  9KB  |  276 lines

  1. /*
  2.  * $XConsortium: ReqMach.c,v 1.10 91/07/24 15:23:52 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Davor Matic, MIT X Consortium
  24.  */
  25.  
  26. #include <X11/StringDefs.h>
  27. #ifdef MSDOS
  28. #include "X11/IntrinsP.h"      /* QDK 05/11/1994 10:44am. */
  29. #else
  30. #include "X11/IntrinsicP.h"
  31. #endif
  32. #include <X11/Xfuncs.h>
  33. #include "BitmapP.h"
  34.     
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <math.h>
  38.  
  39. #ifndef abs
  40. #define abs(x)                        (((x) > 0) ? (x) : -(x))
  41. #endif
  42. #define min(x, y)                     (((x) < (y)) ? (x) : (y))
  43. #define max(x, y)                     (((x) > (y)) ? (x) : (y))
  44.  
  45.  
  46. extern Boolean DEBUG;
  47.  
  48. /*****************************************************************************\
  49.  * Request Machine: stacks up and handles requests from application calls.   * 
  50. \*****************************************************************************/
  51.  
  52. /*
  53.  * Searches for a request record of a request specified by its name.
  54.  * Returns a pointer to the record or NULL if the request was not found.
  55.  */
  56. BWRequestRec *FindRequest(name)
  57.     BWRequest name;
  58. {
  59.     int i;
  60.  
  61.     for (i = 0; i < bitmapClassRec.bitmap_class.num_requests; i++)
  62.     if (!strcmp(name, bitmapClassRec.bitmap_class.requests[i].name))
  63.         return &bitmapClassRec.bitmap_class.requests[i];
  64.     
  65.     return NULL;
  66. }
  67.  
  68. /*
  69.  * Adds a request to the request stack and does proper initializations.
  70.  * Returns TRUE if the request was found and FALSE otherwise.
  71.  */
  72. Boolean BWAddRequest(w, name, trap, call_data, call_data_size)
  73.     Widget    w;
  74.     BWRequest name;
  75.     Boolean   trap;
  76.     caddr_t   call_data;
  77.     Cardinal  call_data_size;
  78. {
  79.     BitmapWidget BW = (BitmapWidget) w;
  80.     BWRequestRec *request;
  81.     
  82.     request = FindRequest(name);
  83.     if(request) {
  84.     if (DEBUG)
  85.       fprintf(stderr, "Adding... Cardinal: %d\n", BW->bitmap.cardinal + 1);
  86.  
  87.     BW->bitmap.request_stack = (BWRequestStack *)
  88.         XtRealloc((char *)BW->bitmap.request_stack,
  89.               (++BW->bitmap.cardinal + 1) * sizeof(BWRequestStack));
  90.     
  91.     BW->bitmap.request_stack[BW->bitmap.cardinal].request = request;
  92.     BW->bitmap.request_stack[BW->bitmap.cardinal].status = 
  93.         XtMalloc(request->status_size);
  94.     BW->bitmap.request_stack[BW->bitmap.cardinal].trap = trap;
  95.     BW->bitmap.request_stack[BW->bitmap.cardinal].call_data = 
  96.         XtMalloc(call_data_size);
  97.     bcopy(call_data, 
  98.           BW->bitmap.request_stack[BW->bitmap.cardinal].call_data,
  99.           call_data_size);
  100.  
  101.     return True;
  102.     }
  103.     else {
  104.     XtWarning("bad request name.  BitmapWidget");
  105.     return False;
  106.     }
  107. }
  108.  
  109. /*
  110.  * Engages the request designated by the current parameter.
  111.  * Returnes TRUE if the request has an engage function and FALSE otherwise.
  112.  */
  113. Boolean Engage(BW, current)
  114.     BitmapWidget BW;
  115.     Cardinal current;
  116. {
  117.     BW->bitmap.current = current;
  118.     
  119.     if (DEBUG)
  120.     fprintf(stderr, "Request: %s\n", 
  121.         BW->bitmap.request_stack[current].request->name);
  122.   
  123.     if (BW->bitmap.request_stack[current].request->engage) {
  124.     (*BW->bitmap.request_stack[current].request->engage)
  125.         ((Widget) BW,
  126.          BW->bitmap.request_stack[current].status,
  127.          BW->bitmap.request_stack[current].request->engage_client_data,
  128.          BW->bitmap.request_stack[current].call_data);
  129.     return True;
  130.     }
  131.     else
  132.     return False;
  133. }
  134.  
  135. Boolean BWTerminateRequest();
  136. Boolean BWRemoveRequest();
  137.  
  138. /*
  139.  * Scans down the request stack removing all requests untill it finds 
  140.  * one to be trapped.
  141.  */
  142. void TrappingLoop(BW)
  143.     BitmapWidget BW;
  144. {
  145.  
  146.     if (DEBUG)
  147.     fprintf(stderr, "Scanning... Current: %d\n", BW->bitmap.current);
  148.     if ((BW->bitmap.current > 0) 
  149.     && 
  150.     (!BW->bitmap.request_stack[BW->bitmap.current--].trap)) {
  151.     BWRemoveRequest((Widget) BW);
  152.     TrappingLoop(BW);
  153.     }
  154.     else
  155.     if (BW->bitmap.cardinal > 0) {
  156.         if (DEBUG)
  157.         fprintf(stderr, "Trapping... Current: %d\n", BW->bitmap.current+1);
  158.         if(!Engage(BW, ++BW->bitmap.current))
  159.         BWTerminateRequest((Widget) BW, True);
  160.     }
  161. }
  162. /*
  163.  * Terimantes the current request and continues with next request if con = TRUE
  164.  * Returnes TRUE if there is any number of requests left on the stack.
  165.  */
  166. Boolean BWTerminateRequest(w, cont)
  167.     Widget w;
  168.     Boolean cont;
  169. {
  170.     BitmapWidget BW = (BitmapWidget) w;
  171.     
  172.     if (BW->bitmap.current > 0) {
  173.     if (DEBUG)
  174.         fprintf(stderr, "Terminating... Current: %d\n", BW->bitmap.current);
  175.         if (BW->bitmap.request_stack[BW->bitmap.current].request->terminate)
  176.         (*BW->bitmap.request_stack[BW->bitmap.current].request->terminate)
  177.         (w,
  178.          BW->bitmap.request_stack[BW->bitmap.current].status,
  179.          BW->bitmap.request_stack[BW->bitmap.current].request->terminate_client_data,
  180.          BW->bitmap.request_stack[BW->bitmap.current].call_data);
  181.     
  182.     if (cont) {
  183.         if (BW->bitmap.current == BW->bitmap.cardinal)
  184.         TrappingLoop(BW);
  185.         else {
  186.         if (DEBUG)
  187.             fprintf(stderr, "Continuing... Current: %d\n", BW->bitmap.current+1);
  188.         if (!Engage(BW, ++BW->bitmap.current))
  189.             BWTerminateRequest(w, True);
  190.         }
  191.     }
  192.     else
  193.         BW->bitmap.current = 0;
  194.     }
  195.     
  196.     return BW->bitmap.current;
  197. }
  198.  
  199. /*
  200.  * Simple interface to BWTerminateRequest that takes only a widget.
  201.  */
  202. void BWAbort(w)
  203.     Widget w;
  204. {
  205.     BWTerminateRequest(w, True);
  206. }
  207.  
  208. /*
  209.  * Removes the top request from the request stack. If the request is active
  210.  * it will terminate it.
  211.  * Returns TRUE if the number of requests left on the stack != 0.
  212.  */
  213. Boolean BWRemoveRequest(w)
  214.     Widget w;
  215. {
  216.     BitmapWidget BW = (BitmapWidget) w;
  217.     
  218.     if (BW->bitmap.cardinal > 0) {
  219.     if (DEBUG)
  220.         fprintf(stderr, "Removing... Cardinal: %d\n", BW->bitmap.cardinal);
  221.     if (BW->bitmap.current == BW->bitmap.cardinal)
  222.         BWTerminateRequest(w, False);
  223.     
  224.     if (BW->bitmap.request_stack[BW->bitmap.cardinal].request->remove)
  225.         (*BW->bitmap.request_stack[BW->bitmap.cardinal].request->remove)
  226.         (w,
  227.          BW->bitmap.request_stack[BW->bitmap.cardinal].status,
  228.          BW->bitmap.request_stack[BW->bitmap.cardinal].request->remove_client_data,
  229.          BW->bitmap.request_stack[BW->bitmap.cardinal].call_data);
  230.     
  231.     XtFree(BW->bitmap.request_stack[BW->bitmap.cardinal].status);
  232.     XtFree(BW->bitmap.request_stack[BW->bitmap.cardinal].call_data);
  233.     BW->bitmap.request_stack = (BWRequestStack *)
  234.         XtRealloc((char *)BW->bitmap.request_stack,
  235.               (--BW->bitmap.cardinal + 1) * sizeof(BWRequestStack));
  236.     
  237.     return True;
  238.     }
  239.     else 
  240.     return False;
  241. }
  242.  
  243. void BWRemoveAllRequests(w)
  244.     Widget w;
  245. {                /* SUPPRESS 530 */
  246.     while (BWRemoveRequest(w)) {/* removes all requests from the stack */}
  247. }
  248.  
  249. /*
  250.  * Adds the request to the stack and performs engaging ritual.
  251.  * Returns TRUE if the request was found, FALSE otherwise.
  252.  */
  253. Boolean BWEngageRequest(w, name, trap, call_data, call_data_size)
  254.     Widget w;
  255.     BWRequest name;
  256.     Boolean trap;
  257.     caddr_t call_data;
  258.     Cardinal call_data_size;
  259. {
  260.     BitmapWidget BW = (BitmapWidget) w;
  261.     
  262.     if (BWAddRequest(w, name, trap, call_data, call_data_size)) {
  263.     BWTerminateRequest(w, False);
  264.     if (DEBUG)
  265.         fprintf(stderr, "Engaging... Cardinal: %d\n", BW->bitmap.cardinal);
  266.     if (!Engage(BW, BW->bitmap.cardinal))
  267.         BWTerminateRequest(w, True);
  268.     
  269.     return True;
  270.     }
  271.     else
  272.     return False;
  273. }
  274.  
  275. /************************* End of the Request Machine ************************/
  276.